package org.ricks.db;

import org.ricks.common.db.ColumnHandler;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ServiceLoader;

/**
 * @author chenwei
 * @Title:
 * @Package
 * @Description: 引用 alibaba fastjson 弃用
 * @date 2020/12/213:42
 */
public class RepositoryBeanProcessor extends BeanProcessor {

    private static final ServiceLoader<ColumnHandler> columnHandlers = ServiceLoader.load(ColumnHandler.class);

    @Override
    protected Object processColumn(ResultSet rs, int index, Class<?> propType) throws SQLException {
        Object retval = rs.getObject(index);

        if ( !propType.isPrimitive() && retval == null ) {
            return null;
        }

        boolean match = false;
        for (ColumnHandler handler : columnHandlers) {
            if (handler.match(propType)) {
                retval = handler.apply(rs, index);
                match = true;
                break;
            }
        }

//        if (!match && propType != Date.class && propType != Enum.class) {
//            retval = JSON.parseObject(rs.getString(index), (Type) propType);
//        }

        return retval;
    }

}
